package com.dk.smsexpress.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.rpc.server.JsonRpcExecutor;
import org.json.rpc.server.JsonRpcServletTransport;

import com.dk.smsexpress.common.ServiceConstant;
import com.dk.smsexpress.common.base.IMobiSmsAutoSelectAccount;
import com.dk.smsexpress.common.base.IMobiSmsManualSelectAccount;
import com.dk.smsexpress.server.imp.MobiSmsAutoSelectAccount;
import com.dk.smsexpress.server.imp.MobiSmsManualSelectAccount;

/**
 * Servlet implementation class ActionHandler
 */
public final class ActionHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private JsonRpcExecutor executor;

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		executor = bind();

	}

	@SuppressWarnings("unchecked")
	private JsonRpcExecutor bind() {
		JsonRpcExecutor executor = new JsonRpcExecutor();

		IMobiSmsManualSelectAccount mobiSmsManual = new MobiSmsManualSelectAccount();
		executor.addHandler(ServiceConstant.MOBIFONE_SMS_MANUAL, mobiSmsManual,
				IMobiSmsManualSelectAccount.class);

		IMobiSmsAutoSelectAccount mobiSmsAuto = new MobiSmsAutoSelectAccount();
		executor.addHandler(ServiceConstant.MOBIFONE_SMS_AUTO, mobiSmsAuto,
				IMobiSmsAutoSelectAccount.class);

		// add more services here
		return executor;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("L.Le 18-Jul-2011");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		executor.execute(new JsonRpcServletTransport(request, response));
	}

}
